package grep_demo

import (
	"context"
	"github.com/go-kit/kit/endpoint"
	grpctransport "github.com/go-kit/kit/transport/grpc"
)

type grpcServer struct {
	sum    grpctransport.Handler
	concat grpctransport.Handler
}

func NewGRPCServer(service endpoint.Endpoint) AddServer {
	return &grpcServer{
		sum: grpctransport.NewServer(
			service,
			decodeGRPCSumRequest,
			encodeGRPCSumResponse,
		),
	}
}

func (s *grpcServer) Sum(ctx context.Context, req *SumRequest) (*SumReply, error) {
	_, rep, err := s.sum.ServeGRPC(ctx, req)
	if err != nil {
		return nil, err
	}
	return rep.(*SumReply), nil
}

func decodeGRPCSumRequest(_ context.Context, grpcReq interface{}) (interface{}, error) {
	req, _ := grpcReq.(*SumRequest)
	return &SumRequest{A: int64(req.A), B: int64(req.B)}, nil
}

func encodeGRPCSumResponse(_ context.Context, response interface{}) (interface{}, error) {
	resp, _ := response.(*SumReply)
	return resp, nil
}
